package com.woniu.product.controller;

import com.woniu.common.util.ResponseData;
import com.woniu.product.pojo.TShoppingcart;
import com.woniu.product.service.TShoppingcartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author 杜梅杰
 * @Description
 * @Date 2024/5/22 13:29
 */
@RestController
public class CartController {
    @Autowired
    private TShoppingcartService shoppingcartService;

    /**
     * 添加购物车
     */
    @PostMapping("/addCart")
    public ResponseData<?> addCart(@RequestBody TShoppingcart shoppingcart) {
        //判断购物车有没有这个商品
        TShoppingcart shopping = shoppingcartService.lambdaQuery()
                .eq(TShoppingcart::getSkuId, shoppingcart.getSkuId())
                .eq(TShoppingcart::getUserId, shoppingcart.getUserId())
                .eq(TShoppingcart::getStatus,0)
                .one();
        if (!ObjectUtils.isEmpty(shopping)) { //如果有就加数量和钱
            shopping.setBuyNum(shopping.getBuyNum()+shoppingcart.getBuyNum());
            shopping.setTotalPrice(shopping.getTotalPrice().add(shoppingcart.getTotalPrice()));
            shoppingcartService.lambdaUpdate()
                    .eq(TShoppingcart::getId,shopping.getId())
                    .update(shopping);
        } else { //没有就填上
            shoppingcart.setCreateTime(new Date());
            shoppingcart.setStatus(0);
            shoppingcartService.save(shoppingcart);
        }
        return new ResponseData<>().ok();
    }

    /**
     * 获取购物车商品
     */
    @PostMapping("/getShoppingcart")
    public ResponseData<?> getShoppingcart(Long userId) {
        return shoppingcartService.getShoppingcart(userId);
    }

    /**
     * 根据id清除购物车商品
     */
    @PostMapping("/removeCart")
    public ResponseData<?> removeCart(Long id,Integer buyNum) {
        TShoppingcart shop = shoppingcartService.lambdaQuery().eq(TShoppingcart::getId, id).one();
        //判断数量
        if (shop.getBuyNum() > buyNum) {
            Integer nowNum = shop.getBuyNum() - buyNum;
            shoppingcartService.lambdaUpdate()
                    .eq(TShoppingcart::getId,id)
                    .setSql("buy_num = " + nowNum)
                    .update();
        } else {
            //如果数量小于就直接将其状态变为取消
            shoppingcartService.lambdaUpdate()
                    .eq(TShoppingcart::getId,id)
                    .setSql("status = 1")
                    .update();
        }

        return new ResponseData<>().ok();
    }

    /**
     * 根据id查询下面购物车商品
     */
    @PostMapping("/queryCartById")
    public TShoppingcart queryCartById(Long id) {
        return shoppingcartService.lambdaQuery()
                .eq(TShoppingcart::getId,id)
                .eq(TShoppingcart::getStatus,0)
                .one();
    }

    /**
     * 修改购物车商品数量,总金额
     */
    @PostMapping("/updateCartCount")
    public void updateCartCount(Long id, Long buyNum, BigDecimal totalPrice) {
        shoppingcartService.lambdaUpdate()
                .setSql("buy_num = " + buyNum)
                .setSql("total_price = " + totalPrice)
                .eq(TShoppingcart::getId,id)
                .eq(TShoppingcart::getStatus,0)
                .update();
    }

    /**
     * 批量修改购物车状态
     */
    @PostMapping("/updateCartStatus")
    public void updateCartStatus(@RequestBody List<TShoppingcart> shops) {
        for (TShoppingcart shop : shops) {
            shop.setStatus(1); // 设置新的状态
        }
        shoppingcartService.updateBatchById(shops);
    }

    /**
     * 查sortId为几的购物车商品数量
     */
    @PostMapping("/queryCartCount")
    public Integer queryCartCount(@RequestParam("sortId") Integer sortId) {
        return shoppingcartService.queryCartCount(sortId);
    }

    @GetMapping("/getShopcartItemCount/{userId}")
    public ResponseData<?> getShopcartItemCount(@PathVariable("userId") Long userId) {
        return shoppingcartService.getShopcartItemCount(userId);
    }
}
